<?php

/**
 * RbacCommand class
 *
 * @author Julot <andy.julot@gmail.com>
 * @since Thursday, June 30, 2011. 01:42 PM
 */
class RbacCommand extends CConsoleCommand {

	public function getHelp() {
		return <<<EOD
USAGE
    rbac
DESCRIPTION
    This command generates an initial RBAC authorization hierarchy.
EOD;
	}

	/**
	 * Execute the action.
	 * @param array command line parameters specific for this command
	 */
	public function run($args) {
		echo "This command will create these roles:\n";
		echo "  1. Admin\n";
		echo "  2. News Moderator\n";
		echo "  3. User Moderator\n";
		echo "  4. Member Moderator\n";
		echo "  5. Gallery Moderator\n";
		echo "  6. Download Moderator\n";
		echo "  6. Slideshow Moderator\n";
		echo "And the following permissions:\n";
		echo "  1. Update, Hide, Unhide, Sticky, Unsticky, Manage and Download News\n";
		echo "  2. Create, List, View, Update, Delete and Manage User\n";
		echo "  3. Create, List, View, Update, Delete and Manage Member\n";
		echo "  4. Create, List, View, Update, Delete and Manage Cardreq\n";
		echo "  5. Upload, Rename, Move, Delete, Add Folder and Manage Gallery\n";
		echo "  6. Upload, Rename, Move, Delete, Add Folder and Manage Download\n";
		echo "  6. Upload, Rename, Move, Delete, Add Folder and Manage Slideshow\n";
		echo "And assign admin roles to user #1\n\n";
		echo "Would you like to continue? [Yes|No] ";
		if (strncasecmp(trim(fgets(STDIN)), 'y', 1)) return;

		$auth = Yii::app()->authManager;
		$auth->clearAll();

		// News Operation
		$auth->createOperation('newsUpdate', 'Update news');
		$auth->createOperation('newsHide', 'Hide news');
		$auth->createOperation('newsUnhide', 'Unhide news');
		$auth->createOperation('newsSticky', 'Sticky news');
		$auth->createOperation('newsUnsticky', 'Unsticky news');
		$auth->createOperation('newsManage', 'Manage news');
		$auth->createOperation('newsDownload', 'Download news in Excel format');

		// User Operation
		$auth->createOperation('userList', 'List user');
		$auth->createOperation('userView', 'View user');
		$auth->createOperation('userCreate', 'Create user');
		$auth->createOperation('userUpdate', 'Update user');
		$auth->createOperation('userDelete', 'Delete user');
		$auth->createOperation('userManage', 'Manage user');
		$auth->createOperation('userRbac', 'Manage user rights');

		// Member Operation
		$auth->createOperation('memberList', 'List member');
		$auth->createOperation('memberView', 'View member');
		$auth->createOperation('memberCreate', 'Create member');
		$auth->createOperation('memberUpdate', 'Update member');
		$auth->createOperation('memberDelete', 'Delete member');
		$auth->createOperation('memberManage', 'Manage member');

		// Cardreq Operation
		$auth->createOperation('cardreqList', 'List card request');
		$auth->createOperation('cardreqView', 'View card request');
		$auth->createOperation('cardreqCreate', 'Create card request');
		$auth->createOperation('cardreqUpdate', 'Update card request');
		$auth->createOperation('cardreqDelete', 'Delete card request');
		$auth->createOperation('cardreqManage', 'Manage card request');

		// Gallery Operation
		$auth->createOperation('galleryUpload', 'Upload gallery');
		$auth->createOperation('galleryRename', 'Rename gallery');
		$auth->createOperation('galleryMove', 'Move gallery');
		$auth->createOperation('galleryDelete', 'Delete gallery');
		$auth->createOperation('galleryAddFolder', 'Add folder gallery');
		$auth->createOperation('galleryManage', 'Manage gallery');

		// Download Operation
		$auth->createOperation('downloadUpload', 'Upload download');
		$auth->createOperation('downloadRename', 'Rename download');
		$auth->createOperation('downloadMove', 'Move download');
		$auth->createOperation('downloadDelete', 'Delete download');
		$auth->createOperation('downloadAddFolder', 'Add folder download');
		$auth->createOperation('downloadManage', 'Manage download');

		// Slideshow Operation
		$auth->createOperation('slideshowUpload', 'Upload slideshow');
		$auth->createOperation('slideshowRename', 'Rename slideshow');
		$auth->createOperation('slideshowMove', 'Move slideshow');
		$auth->createOperation('slideshowDelete', 'Delete slideshow');
		$auth->createOperation('slideshowAddFolder', 'Add folder slideshow');
		$auth->createOperation('slideshowManage', 'Manage slideshow');

		// Office Operation

		// News Moderator
		$role = $auth->createRole('newsModerator', 'News Moderator');
		$role->addChild('newsUpdate');
		$role->addChild('newsHide');
		$role->addChild('newsUnhide');
		$role->addChild('newsSticky');
		$role->addChild('newsUnsticky');
		$role->addChild('newsManage');
		$role->addChild('newsDownload');

		// User Moderator
		$role = $auth->createRole('userModerator', 'User Moderator');
		$role->addChild('userList');
		$role->addChild('userView');
		$role->addChild('userCreate');
		$role->addChild('userUpdate');
		$role->addChild('userDelete');
		$role->addChild('userManage');
		$role->addChild('userRbac');

		// Member Moderator
		$role = $auth->createRole('memberModerator', 'Member Moderator');
		$role->addChild('memberList');
		$role->addChild('memberView');
		$role->addChild('memberCreate');
		$role->addChild('memberUpdate');
		$role->addChild('memberDelete');
		$role->addChild('memberManage');

		// Cardreq Moderator
		$role = $auth->createRole('cardreqModerator', 'KTA Moderator');
		$role->addChild('cardreqList');
		$role->addChild('cardreqView');
		$role->addChild('cardreqCreate');
		$role->addChild('cardreqUpdate');
		$role->addChild('cardreqDelete');
		$role->addChild('cardreqManage');

		// Gallery Moderator
		$role = $auth->createRole('galleryModerator', 'Gallery Moderator');
		$role->addChild('galleryUpload');
		$role->addChild('galleryRename');
		$role->addChild('galleryMove');
		$role->addChild('galleryDelete');
		$role->addChild('galleryAddFolder');
		$role->addChild('galleryManage');

		// Download Moderator
		$role = $auth->createRole('downloadModerator', 'Download Moderator');
		$role->addChild('downloadUpload');
		$role->addChild('downloadRename');
		$role->addChild('downloadMove');
		$role->addChild('downloadDelete');
		$role->addChild('downloadAddFolder');
		$role->addChild('downloadManage');

		// Slideshow Moderator
		$role = $auth->createRole('slideshowModerator', 'Slideshow Moderator');
		$role->addChild('slideshowUpload');
		$role->addChild('slideshowRename');
		$role->addChild('slideshowMove');
		$role->addChild('slideshowDelete');
		$role->addChild('slideshowAddFolder');
		$role->addChild('slideshowManage');

		// Admin Role
		$role = $auth->createRole('admin', 'Administrator');
		$role->addChild('newsModerator');
		$role->addChild('userModerator');
		$role->addChild('memberModerator');
		$role->addChild('cardreqModerator');
		$role->addChild('galleryModerator');
		$role->addChild('downloadModerator');
		$role->addChild('slideshowModerator');

		$auth->assign('admin', 1);

		echo "Authorization hierarchy successfully generated.";
	}

}

